#include "state_free_play_menu.h"

PuzzleSelect::PuzzleSelect(bn::sprite_text_generator* stg) :
    GameState(), bg(buildBG()), map(bg.map()), text_gnrtr(stg),
    menu_hrz_lower_limit(15), menu_hrz_upper_limit(23), menu_vrt_lower_limit(13), menu_vrt_upper_limit(19),
    cursor_sprite(buildSprite(bn::sprite_items::cursor_circle, 0,0)),
    scribble_left(buildSprite(bn::sprite_items::scribble_star, -68, -40))
{
    bg.set_blending_enabled(true);
    scribble_left->set_blending_enabled(true);
    cursor_sprite->set_blending_enabled(true);

    //bn::blending::set_fade_alpha(1);

    for (int i = 0, k = 0; i < slots_occupied.size(); ++i, k+=18)
    {
        slots_occupied[i] = tool::validateSRAMData(k);
        BN_LOG(i, " slot data valid: ", slots_occupied[i]);
    }

    showMenu();
}

void PuzzleSelect::showMenu()
{
    for (int i = 0; i < slots_occupied.size(); ++i)
    {
        if (slots_occupied[i])
        {
            menu_vrt_upper_limit += 2;
            break;
        }
    }

    resetMap();
    PaperSheetPattern_PuzzleSelection(menu_vrt_lower_limit, menu_vrt_upper_limit);
    refreshScreen(map);

    showMenuText();

}

void PuzzleSelect::showMenuText()
{
    symbols.clear();

    text_gnrtr->generate(tool::cellX2Screen(9, 4), tool::cellY2Screen(10, 4), "Choose a puzzle", symbols);

    text_gnrtr->generate(tool::cellX2Screen(6, 4), tool::cellY2Screen(13, 4), "things", symbols);
    text_gnrtr->generate(tool::cellX2Screen(6, 4), tool::cellY2Screen(15, 4), "nature", symbols);
    text_gnrtr->generate(tool::cellX2Screen(6, 4), tool::cellY2Screen(17, 4), "symbols", symbols);
    text_gnrtr->generate(tool::cellX2Screen(6, 4), tool::cellY2Screen(19, 4), "portraits", symbols);

    for (int i = 0; i < slots_occupied.size(); ++i)
    {
        if (slots_occupied[i])
        {
            text_gnrtr->generate(tool::cellX2Screen(6, 4), tool::cellY2Screen(21, 4), "custom", symbols);
            break;
        }
    }
}


bn::array<bool, 144> const& PuzzleSelect::getSelectedPuzzle()
{
    bn::sound_items::ganbatte_ne.play(1);
    switch(cursor_position[1])
    {
        case 13:
            switch(cursor_position[0])
            {
                case 15:
                    return puzzle::Things_Gameboy;
                case 17:
                    return puzzle::Things_Giftbox;
                case 19:
                    return puzzle::Things_Notepad;
                case 21:
                    return puzzle::Things_Chair;
                case 23:
                    return puzzle::Things_Key;
                default:
                    break;
            }
            break;
        case 15:
            switch(cursor_position[0])
            {
                case 15:
                    return puzzle::Nature_4LClover;
                case 17:
                    return puzzle::Nature_Flower;
                case 19:
                    return puzzle::Nature_Lagomorph;
                case 21:
                    return puzzle::Nature_Duck;
                case 23:
                    return puzzle::Nature_Dog;
                default:
                    break;
            }
            break;
        case 17:
            switch(cursor_position[0])
            {
                case 15:
                    return puzzle::Kanji_Cat;
                case 17:
                    return puzzle::Kanji_Congrats;
                case 19:
                    return puzzle::Kanji_Flower;
                case 21:
                    return puzzle::Writing_Hi;
                case 23:
                    return puzzle::Symbol_Clef;
                default:
                    break;
            }
            break;
        case 19:
            switch(cursor_position[0])
            {
                case 15:
                    return puzzle::Portrait_Nyra;
                case 17:
                    return puzzle::Portrait_Tooru;
                case 19:
                    return puzzle::Portrait_Yuuji;
                case 21:
                    return puzzle::Portrait_Steph;
                case 23:
                    return puzzle::Portrait_Yuli;
                default:
                    break;
            }
            break;
        case 21:
            if (slots_occupied[(cursor_position[0]-15)/2] == true)
            {
                tool::UInt2BoolArray(puzzle::Custom, (cursor_position[0]-15)/2*18);
                return puzzle::Custom;
            }
            break;
        default:
            break;
        }
}

int PuzzleSelect::updateState()
{
    if (bn::keypad::down_pressed() && cursor_position[1] < menu_vrt_upper_limit)
    {
        cursor_position[1]+=2;
    }
    else if (bn::keypad::up_pressed() && cursor_position[1] > menu_vrt_lower_limit)
    {
        cursor_position[1]-=2;
    }
    if (bn::keypad::right_pressed() && cursor_position[0] < menu_hrz_upper_limit)
    {
        cursor_position[0]+=2;
    }
    else if (bn::keypad::left_pressed() && cursor_position[0] > menu_hrz_lower_limit)
    {
        cursor_position[0]-=2;
    }

    if (bn::keypad::a_pressed())
    {
        if (cursor_position[1] == 21)
            if (slots_occupied[(cursor_position[0]-15)/2] == false)
            {
                if (!empty_selection)
                {
                    text_gnrtr->generate(tool::cellX2Screen(11, 4), tool::cellY2Screen(23, 4), "no puzzle found!", symbols);
                    empty_selection = true;
                }
                return -1;
            }
        return 4;
    }
    else if (bn::keypad::select_pressed() || bn::keypad::b_pressed())
    {
        return 0;
    }
    two_frame_anim.update();
    cursor_sprite->set_position(tool::cellX2Screen(cursor_position[0], 8), tool::cellY2Screen(cursor_position[1],8));

    if (empty_selection && ++frame_counter >= 100)
    {
        empty_selection = false;
        frame_counter = 0;
        showMenuText();
    }

    return -1;
}

void PuzzleSelect::toggleStateVisibility(bool show)
{
    bg.set_visible(show);
    cursor_sprite->set_visible(show);
    scribble_left->set_visible(show);
    symbols.clear();
    if (show) showMenuText();
}

void PuzzleSelect::toggleFadeEffect()
{

}

PuzzleSelect::~PuzzleSelect()
{
    symbols.clear();
    BN_LOG("free play menu class destructor");
}
